package com.example.rocketmq.controller;

import com.example.rocketmq.entity.Product;
import com.example.rocketmq.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author kangshuai
 */
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/product/{stockNum}")
    public Product order(@PathVariable(value = "stockNum") Integer stockNum) {

        Product product = new Product();
        product.setStockNum(stockNum);
        productService.save(product);

        redisTemplate.opsForValue().set("product:stock:" + product.getProductId(), stockNum.toString());

        return product;
    }
}
